﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
using System.Text;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Module_ManagerController : Controller
    {

        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }


        /// <summary>
        /// 返回功能模块列表
        /// </summary>
        /// <returns>json</returns>
        public string List(string actionId)
        {
            int recordNum = 0;
            QueryParam qp = new QueryParam();
            //返回json数组
            if (!string.IsNullOrEmpty(actionId) && actionId.StartsWith("m"))
            {
                qp.PageIndex = 1;
                qp.PageSize = int.MaxValue;
                qp.Where ="   IsPart!=2 and enabled=1";
                IDataService<Normal_ChannelEntity> Normal_ChannelSv = ServiceBuilder.BuildNormal_ChannelService();
                List<Normal_ChannelEntity> sys_moduleList = Normal_ChannelSv.List(qp, out recordNum);
                StringBuilder sb = new StringBuilder();
                sb.Append("[");
                GetTreeString(sb, 0, sys_moduleList);
                sb.Append("]");
                returnJson = sb.ToString();

            }
            else
            {
                int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
                int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
                int parentId = Utils.StringToInt(actionId,0);
                qp.Orderfld = "OrderNum asc ";
                qp.PageIndex = pageIndex / pageSize + 1; 
                qp.PageSize = pageSize;
                qp.Where = string.Concat(" ReId=", parentId);
                IsoDateTimeConverter jsDataConvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
                jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
                IDataService<Normal_ChannelEntity> Normal_ChannelSv = ServiceBuilder.BuildNormal_ChannelService();
                List<Normal_ChannelEntity> normal_channelList = Normal_ChannelSv.List(qp, out recordNum);
                returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(normal_channelList, Formatting.Indented, jsDataConvert), "}");
     
            }
            return returnJson;

        }

        public string List2()
        {
            int recordNum = 0;
            QueryParam qp = new QueryParam();
            //返回json数组
           
                qp.PageIndex = 1;
                qp.PageSize = int.MaxValue;
                qp.ReturnFields = "Id,Title,Path";
                qp.Where = "   IsPart!=2 and enabled=1 and reid=0";
                IDataService<Normal_ChannelEntity> Normal_ChannelSv = ServiceBuilder.BuildNormal_ChannelService();
                List<Normal_ChannelEntity> sys_moduleList = Normal_ChannelSv.List(qp, out recordNum);
                List<Normal_ChannelEntity> normal_channelList = Normal_ChannelSv.List(qp, out recordNum);
                returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(normal_channelList), "}");
     
                

            return returnJson;

        }

        /// <summary>
        /// 获取树状json 列表
        /// </summary>
        /// <param name="sb">记录容器</param>
        /// <param name="id">起始id</param>
        /// <param name="list">要循环的列表</param>
        protected void GetTreeString2(StringBuilder sb, int id, List<Normal_ChannelEntity> list)
        {
            bool isHasChild = false;
            int intCount = 0;
            int intIndex = 0;
            foreach (Normal_ChannelEntity module in list)
            {
                if (module.ReId == id)
                {
                    intCount++;
                }
            }

            if (intCount != 0)
            {
                foreach (Normal_ChannelEntity module in list)
                {
                    isHasChild = false;
                    if (module.ReId == id)
                    {

                        intIndex++;

                        sb.Append(string.Concat("{text:'", module.Title, "',id:'", module.Path, "',channelType:'", module.ChannelType, "',isPart:'", module.IsPart, "'"));
                        //if (module.IsPart == 0)
                        //{
                        //    sb.Append(",cls:'x-tree-node-italic-light'");
                        //}
                        foreach (Normal_ChannelEntity module2 in list)
                        {
                            if (module2.ReId == module.Id)
                            {
                                isHasChild = true;
                                if (list.Count > 22)
                                    sb.Append(",expanded:false,children:[");
                                else
                                    sb.Append(",expanded:true,children:[");
                                break;
                            }
                        }
                        GetTreeString(sb, module.Id, list);

                        if (isHasChild)
                        {
                            if (intIndex != intCount)
                            {

                                sb.Append("],leaf:true},");
                            }
                            else
                            {
                                sb.Append("],leaf:true}");
                            }
                        }
                        else
                        {
                            if (intIndex != intCount)
                            {
                                sb.Append(",leaf:true},");
                            }
                            else
                            {
                                sb.Append(",leaf:true}");
                            }
                        }

                    }

                }
            }
        }
        /// <summary>
        /// 获取树状json 列表
        /// </summary>
        /// <param name="sb">记录容器</param>
        /// <param name="id">起始id</param>
        /// <param name="list">要循环的列表</param>
        protected void GetTreeString(StringBuilder sb, int id, List<Normal_ChannelEntity> list)
        {
            bool isHasChild = false;
            int intCount = 0;
            int intIndex = 0;
            foreach (Normal_ChannelEntity module in list)
            {
                if (module.ReId == id)
                {
                    intCount++;
                }
            }

            if (intCount != 0)
            {
                foreach (Normal_ChannelEntity module in list)
                {
                    isHasChild = false;
                    if (module.ReId == id)
                    {

                        intIndex++;

                        sb.Append(string.Concat("{text:'", module.Title, "',id:'", module.Id,  "',channelType:'", module.ChannelType, "',isPart:'", module.IsPart, "'"));
                        if (module.IsPart == 0)
                        {
                            sb.Append(",cls:'x-tree-node-italic-light'");
                        }
                        foreach (Normal_ChannelEntity module2 in list)
                        {
                            if (module2.ReId == module.Id)
                            {
                                isHasChild = true;
                                if (list.Count > 22)
                                    sb.Append(",expanded:false,children:[");
                                else
                                    sb.Append(",expanded:true,children:[");
                                break;
                            }
                        }
                        GetTreeString(sb, module.Id, list);

                        if (isHasChild)
                        {
                            if (intIndex != intCount)
                            {

                                sb.Append("]},");
                            }
                            else
                            {
                                sb.Append("]}");
                            }
                        }
                        else
                        {
                            if (intIndex != intCount)
                            {
                                sb.Append(",leaf:true},");
                            }
                            else
                            {
                                sb.Append(",leaf:true}");
                            }
                        }

                    }

                }
            }
        }
        /// <summary>
        /// 查看频道 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
            return returnJson;
        }

        /// <summary>
        /// 更新频道
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            return returnJson;
        }

        /// <summary>
        /// 新增频道
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            return returnJson;
        }

        /// <summary>
        /// 删除频道
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            return returnJson;
        }

        /// <summary>
        /// 批量删除频道
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {

            return returnJson;
        }

    }
}
